<?php
/**
 * User: Digvijay
 * Date: 3/17/14
 * Time: 11:45 AM
 */

class Dashbord extends MY_Controller {
    function index() {
        $noOfNewOrder = $this->getNoOfOrdersByType(Order::ORDER_PENDING);
        $noOfNewAgents = $this->getNoAgentsByType(Agency::STATUS_PENDING);
        $noOfProducts = $this->getNoOfProducts();
        $data = array("pageTitle" => "Dashbord", "newOrders" => $noOfNewOrder, "newAgents" => $noOfNewAgents, "noOfProducts"=>$noOfProducts);
        $this->load->view("admin/dashbord", $data);
    }

    function agentsVsAnonymous() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $noOfOrderByAgents = $this->getNoOfOrderByAgentType(Agency::CLIENT_AGENT);
            $noOfOrdersByAnonymous = $this->getNoOfOrderByAgentType(Agency::CLIENT_ANONYMOUS);

            $chart = array("agents" => $noOfOrderByAgents, "anonymous" => $noOfOrdersByAnonymous);
            $response->setData($chart);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    function orderTypes() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $noOfNewOrders = $this->getNoOfOrdersByType(Order::ORDER_PENDING);
            $noOfCompletedOrders = $this->getNoOfOrdersByType(Order::ORDER_DELIVERED);
            $noOfCancelOrders = $this->getNoOfOrdersByType(Order::ORDER_CANCELLED);

            $chart = array("new" => $noOfNewOrders, "completed" => $noOfCompletedOrders, "cancel" => $noOfCancelOrders);
            $response->setData($chart);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    function getSalesForYear($year = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            if ($year == null) {
                $year = date('Y');
            }
            $monthName = array("Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
            $yearReport = array();
            for ($i = 0; $i < 12; $i++) {
                $month = array("month"=> $monthName[$i], "sales" =>$this->getSalesByMonth($i, $year));
                array_push($yearReport,$month);
            }
            $response->setData($yearReport);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
}
